<?php

namespace app\index\controller;

use app\admin\library\Auth;
use app\common\controller\Frontend;
use app\admin\model\Admin;
use app\admin\model\serial;
use app\common\model\Agent;
use think\Db;
class Send extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        $this->model = new \app\common\model\Serial;
        parent::_initialize();
    }


    public function index()
    {
        if ($this->request->isPost()) {
            $post = $_POST;
            $agent = Agent::get(['agent_phone'=>$post['username']]);
            if($agent && $agent->password == md5($_POST['password'])){

                if($agent->send_country==1){
                    $this->error("对不起，您不能发往全国");
                }
                $this->success("登陆成功",'/index/send/send/?id='.$agent->id);
            }else{
                $this->error('登陆失败,密码错误');
            }
        }
        return $this->view->fetch();
    }
    public function send()
    {
        $id = $this->request->get("id") ;
        if(!$id) $this->error("请先登录","/index/send/index");
        //获取代理商信息
        $agent = Agent::get(['id'=>$id]);

        if ($this->request->isPost()) {
            //接收序列号
            $row = $this->request->request('row/a');
            //封装数组
            $codeListArr = explode("\n", $row['code']);
            //去除空数组
            $ptn = "/\S+/i";
            $codeListArr = preg_grep($ptn, $codeListArr);
            if (!$codeListArr) $this->error('请输入序列号信息');
            $area='';
            $province='';
            $city='';
            //获取地区信息
            $diqu = array('省','自治区','壮族','回族','维吾尔','特别行政区');
            if($this->request->post("province") && $this->request->post("city") && $this->request->post("area")){
                $province = str_replace($diqu,'',$this->request->post("province"));
                $city = str_replace('市','',$this->request->post("city"));
                $area = $this->request->post("area");
            }elseif($this->request->post("province") && $this->request->post("city")){

                $province = str_replace($diqu,'',$this->request->post("province"));
                $city = $this->request->post("city");
                $area='';
            }elseif($this->request->post("province")){
                $province = str_replace(array('自治区','壮族','回族','维吾尔','特别行政区'),'',$this->request->post("province"));
                $city='';
                $area='';
            }
            //开启事务
            Db::startTrans();
            foreach ($codeListArr as $ve){
                //去除符号和空格
                $ve = str_replace("-", "", str_replace(" ",'', trim($ve)));
                preg_match_all('/\d+/',$ve,$arr);
                $ve = join('',$arr[0]);
                $codeArr =$this->model->where(['code'=>$ve])->find();
                if(!$codeArr)$this->error('序列号不存在' .$ve);
                if($codeArr['is_cancel']==1)$this->error('序列号已作废:' .$ve);
                if($codeArr['is_register']==1)$this->error('序列号已被用户激活:' .$ve);
                if($codeArr['dealer_uid']!=$id)$this->error('当前序列号不属于此经销商:' .$ve);
                //if($codeArr['send']==1)$this->error('序列号已发货' .$ve);
                //代理商
                $data['dealer_uid'] = $agent->id;
                $data['send'] = 1;
                $data['sendtime'] = time();
                $data['kaoshi']= $province;
                $data['fahuo'] = $province.$city.$area;

                $send_show = $this->model->where(['code'=>$ve])->update($data);
                if(!$send_show){
                    Db::rollback();
                    $this->error('发货失败');
                }

            }
            // 提交事务
            Db::commit();
            $this->success('发货成功' . count($codeListArr) . '个');
        }
        $this->view->assign('id', $id);
        return $this->view->fetch();
    }

    public function update_book(){
        //查询更新数据
        $data = Db::query("select code,fahuo,kaoshi,dealer_uid,branch_uid,sendtime from hdyj_serial_insert where status=1");

        foreach ($data as $key=>$value){
            $list['fahuo'] = $value['fahuo'];
            $list['kaoshi'] = $value['kaoshi'];
            $list['dealer_uid'] = $value['dealer_uid'];
            $list['branch_uid'] = $value['branch_uid'];
            $list['sendtime'] = $value['sendtime'];
            $list['send'] = 1;
            $this->model->where(['code'=>$value['code']])->update($list);
            //更新存档表状态
            DB::query("update hdyj_serial_insert set status=2 where code=".$value['code']);
        }
    }


    public function del_book(){
        //定时删除附表数据
        Db::query('delete from hdyj_serial_insert');
    }

}
